package br.com.virtual.base.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.inject.Inject;
import javax.inject.Named;

import br.com.virtual.base.uteis.VirtualVerificador;
import br.com.virtual.cadastrar.model.Categoria;
import br.com.virtual.cadastrar.service.CategoriaService;

@Named("categoriaConverter")
public class CategoriaConverter implements Converter {

	@Inject
	CategoriaService categoriaService;

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {

		if (arg2 == null || arg2.trim().length() == 0 || !VirtualVerificador.isNumeric(arg2) )
			return null;

		return categoriaService.read(Long.valueOf(arg2));

	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {

		Categoria c = (Categoria) arg2;
		if (arg2 == null || c.getId() == null)
			return null;

		return c.getId().toString();
	}

}
